import 'package:flutter/material.dart';

import 'package:charts_flutter/flutter.dart' as charts;
import 'package:drm/base/tj_date_entity.dart';
import 'package:drm/util/utils.dart';
import 'package:drm/util/utils.dart';

class FourPage extends StatefulWidget {
  List<TjDateEntity> data;

  FourPage({Key key, this.data}) : super(key: key);

  @override
  _FourPageState createState() => _FourPageState();
}

class _FourPageState extends State<FourPage> {

  @override
  Widget build(BuildContext context) {
    return charts.BarChart(
      _createSampleData(),
      animate: true,
      vertical: false,
      barRendererDecorator: new charts.BarLabelDecorator<String>(),
      domainAxis:
      new charts.OrdinalAxisSpec(renderSpec: new charts.SmallTickRendererSpec(

      )),
      barGroupingType: charts.BarGroupingType.grouped,
      behaviors: [new charts.SeriesLegend(
        position: charts.BehaviorPosition.top,

      )],


    );
  }


  charts.Series<TjDateEntity, String> iniRow(String title,List<TjDateEntity> data,charts.Color color) {
    return charts.Series<TjDateEntity, String>(
      id: title,
      domainFn: (TjDateEntity sales, _)=>MyUtils.getChartLeftTitle(sales.name),
      measureFn: (TjDateEntity sales, _) {
        try {
          if (sales.value.isEmpty) {
            return 0 ;
          }else {
            return double.parse(sales.value);
          }

        } catch (e) {
          return 0;
        }
      },
      data: data,
      labelAccessorFn: (TjDateEntity sales, _) => '${sales.value.isEmpty?'0':MyUtils.intFormat(sales.value)}',
      fillColorFn: (_, __) =>color,
      colorFn: (TjDateEntity sales, _) =>color,
    );
  }


  List<charts.Series<TjDateEntity, String>> _createSampleData() {
    List<TjDateEntity> xxDate = <TjDateEntity>[];
    List<TjDateEntity> thDate = <TjDateEntity>[];

    List<TjDateEntity> datas = widget.data;
    datas.forEach((value) {
      xxDate.add(TjDateEntity(name: value.name, value: value.value));
      thDate.add(TjDateEntity(name: value.name, value: value.value1));
    });

    return [
      iniRow('销售',xxDate,charts.MaterialPalette.blue.shadeDefault),
      iniRow('退货',thDate,charts.MaterialPalette.deepOrange.shadeDefault),
    ];
  }
}
